<?php


namespace app\common\library;


use common;

class Hiflux {

    static function _query($dbcfg, $measurement, $selectlist, $where, $pos, $limit) {
        $fields = array_keys($selectlist);
        $fs = implode(',', $fields);
        $st = "select $fs from $measurement where {$where}" . " limit {$limit} offset {$pos} tz('Asia/Shanghai') ";

        $url = "{$dbcfg['host']}/query";
        $params = [
            'db' => $dbcfg['db'],
            'u' => $dbcfg['username'],
            'p' => $dbcfg['passwd'],
            'epoch' => 's',
            'q' => $st,
        ];

        $result = common::curlgetjson($url, $params);

        // hilog::debug($result);

        $l = [];
        if ($result & isset($result['results']) && count($result['results']) > 0) {
            $r0 = $result['results'][0];
            if (isset($r0['series']) && count($r0['series']) > 0) {
                $values = $r0['series'][0]['values'];
                foreach ($values as $v) {
                    $item = [];
                    for ($i = 0; $i < count($fields); $i++) {
                        if (is_null($v[$i + 1])) {
                            continue;
                        }

                        $f = $fields[$i];
                        $item[$f] = $v[$i + 1];
                    }
                    $item = common::type_conver($item, $selectlist);
                    $item['time'] = $v[0];
                    $l[] = $item;
                }
                // hilog::debug('################asdf#', $l);
            }
        }
        return $l;
    }

    static function query($measurement, $selectlist, $where, $pos, $limit) {
        $cfx = get_vxk_config()['hiflux'];
        return self::_query($cfx, $measurement, $selectlist, $where, $pos, $limit);
    }

    static function _insert($dbcfg, $measurement, $tags, $fields, $timens) {
        $url = "{$dbcfg['host']}/write?";
        $params = [
            "db=" . rawurlencode($dbcfg['db']),
            "u=" . rawurlencode($dbcfg['username']),
            "p=" . rawurlencode($dbcfg['passwd']),
        ];
        $url = $url . implode('&', $params);

        $tarr = [];
        foreach ($tags as $k => $v) {
            $tarr[] = "$k=$v";
        }


        $farr = [];
        foreach ($fields as $k => $v) {
            if (is_null($v) || $v === '') {
                continue;
            }

            if (is_object($v) || is_array($v)) {
                $v = json_encode($v);
            }

            if (is_string($v)) {
                $farr[] = "$k=\"$v\"";
            } else {
                $farr[] = "$k=$v";
            }
        }
        if ($timens < 0) {
            $timens = common::time_ns();
        }
        $pdata = $measurement . ',' . implode(',', $tarr) . " " . implode(',', $farr) . ' ' . $timens;
        common::curlpost($url, $pdata, []);
    }

    static function _insert_multi($dbcfg, $measurement, $points) {
        $url = "{$dbcfg['host']}/write?";
        $params = [
            "db=" . rawurlencode($dbcfg['db']),
            "u=" . rawurlencode($dbcfg['username']),
            "p=" . rawurlencode($dbcfg['passwd']),
        ];
        $url = $url . implode('&', $params);
        $pstrs = [];
        foreach ($points as $point) {
            $tags = $point['tags'];
            $fields = $point['fields'];
            $tarr = [];
            foreach ($tags as $k => $v) {
                $tarr[] = "$k=$v";
            }
            $farr = [];
            foreach ($fields as $k => $v) {
                if (is_null($v) || $v === '') {
                    continue;
                }
                if (is_object($v) || is_array($v)) {
                    $v = json_encode($v);
                }
                if (is_string($v)) {
                    $farr[] = "$k=\"$v\"";
                } else {
                    $farr[] = "$k=$v";
                }
            }
            $timens = 0;
            if (isset($point['ns'])) {
                $timens = $point['ns'];
            } else {
                $timens = common::time_ns();
            }
            $pstrs[] = $measurement . ',' . implode(',', $tarr) . " " . implode(',', $farr) . ' ' . $timens;
        }
        $pdata = implode("\n", $pstrs);

        common::curlpost($url, $pdata, []);
    }

    static function insert($measurement, $tags, $fields, $timens = -1) {
        $cfx = get_vxk_config()['hiflux'];
        self::_insert($cfx, $measurement, $tags, $fields, $timens);
    }

    static function insert_muilt($measurement, $points) {
        $cfx = get_vxk_config()['hiflux'];
        self::_insert_multi($cfx, $measurement, $points);
    }

}
